home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / Developer Documentation / Recipes, Tech Notes & Articles / Tech Notes / Utilities Documentation / Printer < prev    next >
Encoding:
Text File  |  1995-11-09  |  5.9 KB  |  100 lines  |  [TEXT/ttxt]

  1. OpenDoc™ Utilities
  2.  
  3.  
  4. Simple Printing Support For Part Editors
  5. by Steve Smith, Thomas Weisbach and Jens Alfke
  6. 9 November 1995
  7.  
  8.  
  9. © 1995 Apple Computer, Inc. All Rights Reserved.
  10. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  11. Mac and OpenDoc are trademarks of Apple Computer, Inc. 
  12.  
  13.  
  14. What it Is
  15.  
  16. The CPrinter class provides basic support for printing OpenDoc frames. It's pretty simple-minded and doesn't support fancy features like frame-per-page printing (as described in the Printing recipe.) It simply takes the frame you give it, tiles it into page-sized rectangles, and prints each one on a page. It does, however, support both QuickDraw and QuickDraw GX printing, which is essential if your part is to print properly (i.e. not crash!) on a machine with GX installed.
  17.  
  18. This code should not be treated as holy writ! If it doesn't do what you want (see above) you are welcome to add functionality to it. You could even just study it to determine how it works (particularly the OpenDoc related code) and then write your own printing code.
  19.  
  20. Features
  21.  
  22. • Supports both the classic (QuickDraw) and GX printing managers. GX users get the neat-o new printing dialogs.
  23. • Page setups is stored persistently (in the kODPropPageSetup property of the part.)
  24. • E-Z API — just add four simple calls to your code. It's much easier than supporting printing in an app!
  25.  
  26. The API
  27.  
  28. Here's the public part of the API, from <Printer.h>:
  29.  
  30. class CPrinter {
  31.     public:
  32.     
  33.         // -- Factory method & destructor --
  34.         static CPrinter*    New( Environment*, ODStorageUnit* );
  35.         virtual ~CPrinter();
  36.         
  37.         // -- Storage --
  38.                 void        Externalize( Environment*, ODStorageUnit* =kODNULL );
  39.     
  40.         // -- Getters --
  41.         virtual ODGraphicsSystem    GetGraphicsSystem( );
  42.                 ODPlatformPrintJob    GetPlatformPrintJob( Environment* );
  43.         virtual ODRect                GetPageRect( Environment* );
  44.     
  45.         // -- Printing --
  46.                 ODBoolean    PageSetup( Environment* );
  47.                 void        PrintDocument( Environment*, ODFrame* initiator, ODShape* area =kODNULL );
  48. };
  49.  
  50. How To Use It
  51.  
  52. First off, you must link the printing code into your editor by adding the source files Printer.cpp, PrinterQD.cpp and PrinterGX.cpp to your project/makefile.
  53. • You must be using the Except utility and be prepared to catch exceptions thrown by the printing code.
  54. • You will need to link against the QuickDrawGXLib shared library. Make this a "weak" import unless your part already requires GX in order to run. See your development system documentation for details on how to weak-import a CFM shared library.
  55.  
  56. The utility consists of a single C++ class called CPrinter. Each instance of your part should have its own CPrinter object. The natural way to do this is to add an instance variable of type “CPrinter*” to your part class or its C++ wrapper. Here is a list of which methods of your part need to talk to the printer object:
  57.  
  58. In InitPart and InitPartFromStorage
  59. Create a new CPrinter object by calling the factory method, and attach it to your part:
  60.     fPrinter = CPrinter::New(ev, storageUnit);
  61. (You can't just say “new CPrinter” because you need to instantiate a different type of printer — a different subclass — depending on whether or not GX is installed.)
  62.  
  63. In Externalize
  64. Unless the draft is read-only, call the Externalize method of the printer object to update the page-setup property of the part's storage unit, if the user has changed the page setup. If you keep a separate flag to determine whether or not your content is dirty and needs to be saved, you should ignore this flag and always call Externalize on the printer anyway, since the page setup is not part of your content and might change independently. There's no need to externalize your entire content just because the page setup changed!
  65.     if( ODGetDraft(ev,storageUnit)->GetPermissions(ev) >= kODDPSharedWrite )
  66.         fPrinter->Externalize(ev);
  67.  
  68. In CloneInto
  69. Again, tell the printer object to Externalize — passing in the target storage unit — so the page setup gets cloned.
  70.     fPrinter->Externalize(ev,targetStorageUnit);
  71.  
  72. In AdjustMenus
  73. Don't forget to enable the Page Setup and Print commands when you're the root part. (You need to talk to the currently installed menu bar, not your own menu bar, since you might not have the menu focus; you're just being called because you're the root part.)
  74.     TempODMenuBar mbar = windowstate->AcquireCurrentMenuBar(ev);
  75.     mbar->EnableCommand(ev,kODCommandPageSetup,kODTrue);
  76.     mbar->EnableCommand(ev,kODCommandPrint,kODTrue);
  77.  
  78. In HandleEvent
  79. Handle the Page Setup and Print commands. In response to Page Setup, call the printer object's PageSetup method. If it returns true, the page setup has been changed and you should call SetChangedFromPrev on the draft to mark it as changed. If you have a private "dirty" flag for your content, you don't need to set it, since the page setup is not part of your content. There's no need to externalize your entire content just because the page setup changed!
  80.     case kODCommandPageSetup:
  81.         if( fPrinter->PageSetup(ev) )
  82.             ODGetDraft(ev,somSelf)->SetChangedFromPrev(ev);
  83.         break;
  84.     case kODCommandPrint:
  85.         fPrinter->PrintDocument(ev,frame);   // Use the frame passed to HandleEvent.
  86.         break;
  87.  
  88. In ReleaseAll
  89. Don't forget to delete the printer object.
  90.     ODDeleteObject(fPrinter);
  91. (The printer object acquires a reference to the part's storage unit, so it's best to delete the printer object in the ReleaseAll method and not wait 'til the part's destructor is called.)
  92.  
  93. Acknowlegements & Bibliography
  94.  
  95. The original printing code was written by Steve Smith. Thomas Weisbach added QuickDraw GX support. Jens Alfke cleaned up the class structure and API, made the code more robust, and wrote the documentation. All of us paid close attention to the following:
  96.  
  97. Macintosh Tech Note PR10 A Printing Loop That Cares.
  98.         <http://www.info.apple.com/dev/technotes/Printing/pr_10.html>
  99. Adding QuickDraw GX  Printing To QuickDraw Applications by Dave Hersey, in 'develop' issue 19, September 1994.
  100. OpenDoc Recipe “Printing”. On the OpenDoc CD.